package com.qf.controller;

import com.qf.pojo.UserInfo;
import com.qf.pojo.resp.BaseResult;
import com.qf.service.impl.UserServiceImpl;
//import com.qf.utils.RedisUtil;
//import com.qf.vo.ActionResult;
//import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserServiceImpl userService;
//    private RedisUtil redisUtil;

    @CrossOrigin
    @RequestMapping("/login")
    public BaseResult login (@RequestBody UserInfo userInfo, HttpSession session){
        //System.out.println(userInfo);
        BaseResult result = userService.login(userInfo,session);
        return result;

//        //        1、根据数据校验用信息
//        UserInfo userInfo1 = userService.findUserByTel(userInfo.getTel());
//
//        if(userInfo1!=null&&userInfo1.getPassword().equals(userInfo.getPassword())){
////            创建令牌,以令牌为key 存入用户权限信息
//            String token = JwtUtil.sign(userInfo.getUsername());
//            log.info("token:"+token);
////        2、后端存储用户状态及权限信息到redis中
//            redisUtil.set(token,userInfo.getUsername());
////        3、，发送客户端
//            return new ActionResult(200,"login success",token);
//        }
//
//        return  new ActionResult(-200,"login fail",null);
    }

    @RequestMapping("/regist")
    public BaseResult regist(@RequestBody UserInfo userInfo){
        BaseResult result = userService.addUser(userInfo);
        return result;
    }
    @RequestMapping("/logout")
    public BaseResult logout(HttpSession session){
        BaseResult result = userService.logout(session);
        return result;
    }

    @RequestMapping("/update")
    public BaseResult update(@RequestBody UserInfo userInfo,HttpSession session){
        BaseResult result = userService.updateUser(userInfo,session);
        return result;
    }

    @RequestMapping("/delete")
    public BaseResult delete(@RequestBody Integer id){
        BaseResult result = userService.deleteUserById(id);
        return result;
    }
    @RequestMapping("/findAll")
    public BaseResult findAll(){
        BaseResult result = userService.findAllUser();
        return result;
    }
    @RequestMapping("/findOneUser")
    public BaseResult findOneBySession(HttpSession session){
        BaseResult result = userService.findOneBySession(session);
        return result;
    }

}
